﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Praxis.Entities;

namespace Praxis.Repository
{
    public class ClienteDAO : MasterRepository, IClienteDAO
    {
        public List<CLIENTE> GetAllClientes()
        {
            var query = from c in Context.CLIENTEs
                        select c;
            return query.ToList();
        }

        public List<CLIENTE> GetFromClienteByDescripcion(string descripcion)
        {
            if (descripcion != "" && descripcion != null && descripcion != string.Empty)
            {
                var query = from c in Context.CLIENTEs
                            where c.NombreRazonSocial.ToUpper().Contains(descripcion.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.CLIENTEs
                             select c;
                return query1.ToList();
            }
        }

        public CLIENTE GetFromClienteById(int id)
        {
            return Context.CLIENTEs.Find(id);
        }

        public void AddCliente(CLIENTE cliente)
        {
            Context.CLIENTEs.Add(cliente);
            Context.SaveChanges();
        }

        public void UpdateCliente(CLIENTE cliente)
        {
            Context.Entry(cliente).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }




        public List<CLIENTE> GetFromClienteByRuc(string ruc)
        {
            if (ruc != "" && ruc != null && ruc != string.Empty)
            {
                var query = from c in Context.CLIENTEs
                            where c.RucDni.ToUpper().Contains(ruc.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.CLIENTEs
                             select c;
                return query1.ToList();
            }
        }


        public bool ExistDuplicidadCliente(string RucDni, string NombreRazonSocial, string Direccion, string Telefono, 
            string Fax, string Contacto, string CelularContacto, string Email, bool Recomendable, string Comentario, int Codigo)
        {
            var query = from c in Context.CLIENTEs
                        where c.RucDni.ToUpper().CompareTo(RucDni.ToUpper()) == 0 &&
                              c.NombreRazonSocial.ToUpper().CompareTo(NombreRazonSocial.ToUpper()) == 0 &&
                              c.Direccion.ToUpper().CompareTo(Direccion.ToUpper()) == 0 &&
                              c.Telefono.ToUpper().CompareTo(Telefono.ToUpper()) == 0 &&
                              c.Fax.ToUpper().CompareTo(Fax.ToUpper()) == 0 &&
                              c.Contacto.ToUpper().CompareTo(Contacto.ToUpper()) == 0 &&
                              c.CelularContacto.ToUpper().CompareTo(CelularContacto.ToUpper()) == 0 &&
                              c.Email.ToUpper().CompareTo(Email.ToUpper()) == 0 &&
                              c.Recomendable.CompareTo(Recomendable) == 0 &&
                              c.Comentario.ToUpper().CompareTo(Comentario.ToUpper()) == 0 &&
                              c.Codigo != Codigo
                        select c;

            return query.Any();
        }


        public CLIENTE GetAllFromClienteByRucDni(string ruc)
        {
            var query = from u in Context.CLIENTEs
                        where u.RucDni.Equals(ruc)
                        select u;
            return query.SingleOrDefault();
        }
    }
}